﻿using Abp.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using shenghua.Common.CustomValueType;
using shenghua.Order.Orders.Median;
using shenghua.Payment.Payments;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Order.Orders
{
    /// <summary>
    /// 订单
    /// </summary>
    public class Order: Entity
    {
        /// <summary>
        /// 订单编号
        /// </summary>
        public string OrderNumber { get; set; }

        public OrderMemberInfo MemberInfo { get; set; }
        public OrderInfo Info { get; set; }
        public OrderShipInfo ShipInfo { get; set; }
        public OrderPaymentInfo PaymentInfo { get; set; }
        public OrderEvaluateInfo EvaluateInfo { get; set; }


        public Order()
        {
            OrderNumber = Order.GenerateOrderNumber();
            Info = new OrderInfo();

            MemberInfo = new OrderMemberInfo();
            ShipInfo = new OrderShipInfo();
            PaymentInfo = new OrderPaymentInfo();
            EvaluateInfo = new OrderEvaluateInfo { };
        }

        public static string GenerateOrderNumber()
        {
            return DateTime.Now.Ticks.ToString();
        }
    }

    [Owned]
    public class OrderMemberInfo
    {
        public long Id { get; set; }
        public string UserName { get; set; }
    }

    [Owned]
    public class OrderShipInfo
    {
        public int DeliveryAddressId { get; set; }
        public int ShippingTemplateId { get; set; }
        public string ExpressName { get; set; }
        public string PersonName { get; set; }
        public string PhoneNumber { get; set; }
        public string Province { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string DetailedAddress { get; set; }
        public string Address
        {
            get
            {
                return $"{Province} {City} {County} {DetailedAddress}";
            }
        }
        public OrderShipWuliuType WuliuType { get; set; }
        public string CustomWuliuInfo { get; set; }
        public OrderShipWuliu Wuliu { get; set; }
        public OrderShipStatus ShipStatus { get; set; }

        public OrderShipInfo()
        {
            Wuliu = new OrderShipWuliu();
        }

        public enum OrderShipWuliuType
        {
            Default = 0,
            Custom = 1
        }

        [Owned]
        public class OrderShipWuliu : IExtendableObject
        {
            public string Number { get; set; }
            public string Type { get; set; }
            public string ExpressName { get; set; }
            [NotMapped]
            public List<OrderShipWuliuItem> Items
            {
                get
                {
                    return this.GetData<List<OrderShipWuliuItem>>("ordershipwuliuitem");
                }
            }
            /// <summary>
            /// 最后一次更新物流信息时间
            /// </summary>
            public DateTime LastUpdateTime
            {
                get
                {
                    return this.GetData<DateTime>("lastupdatetime");
                }
            }
            public string ExtensionData { get; set; }

            public OrderShipWuliu()
            {
                SetItems(new List<OrderShipWuliuItem>());
            }

            public void SetItems(List<OrderShipWuliuItem> items)
            {
                this.SetData("ordershipwuliuitem", items);
                this.SetData("lastupdatetime", DateTime.Now);
            }
        }
        
        public class OrderShipWuliuItem
        {
            public string Time { get; set; }
            public string Description { get; set; }
        }
    }

    [Owned]
    public class OrderEvaluateInfo
    {
        public double RateValue { get; set; }
        public string Description { get; set; }
        public ValueArray<string> Pictures { get; set; }
        public OrderEvaluateStatus EvaluateStatus { get; set; }
        public DateTime EvaluateTime { get; set; }

        public OrderEvaluateInfo()
        {
            Pictures = new ValueArray<string>();
        }
    }

    [Owned]
    public class OrderPaymentInfo
    {
        public PaymentType PaymentType { get; set; }
        public PaymentMethod? PaymentMethod { get; set; }
        public string TradeNumber { get; set; }
        public OrderPaymentStatus PaymentStatus { get; set; }
        public string Remark { get; set; }
    }

    /// <summary>
    /// 订单评价状态
    /// </summary>
    public enum OrderEvaluateStatus
    {
        /// <summary>
        /// 待评价
        /// </summary>
        PendingEvaluate = 0,
        /// <summary>
        /// 已评价
        /// </summary>
        Evaluated = 1
    }

    /// <summary>
    /// 订单支付状态
    /// </summary>
    public enum OrderPaymentStatus
    {
        /// <summary>
        /// 待付款
        /// </summary>
        PendingPay = 0,
        /// <summary>
        /// 正在付款
        /// </summary>
        Paying = 1,
        /// <summary>
        /// 已付款
        /// </summary>
        Paid = 2,
        /// <summary>
        /// 待退款
        /// </summary>
        PendingRefund = 10,
        /// <summary>
        /// 正在退款
        /// </summary>
        Refunding = 11,
        /// <summary>
        /// 已退款
        /// </summary>
        Refunded = 12
    }

    /// <summary>
    /// 订单货运状态
    /// </summary>
    public enum OrderShipStatus
    {
        /// <summary>
        /// 等待发货
        /// </summary>
        PendingShip = 0,
        /// <summary>
        /// 已发货
        /// </summary>
        Shipped = 1,
        /// <summary>
        /// 待收货
        /// </summary>
        PendingReceive = 10,
        /// <summary>
        /// 已收货
        /// </summary>
        Received = 11,
        /// <summary>
        /// 等待退货
        /// </summary>
        PendingReturn = 20,
        /// <summary>
        /// 正在退货
        /// </summary>
        Returning = 21,
        /// <summary>
        /// 已退货
        /// </summary>
        Returned = 22
    }

    public enum PaymentType
    {
        /// <summary>
        /// 在线支付
        /// </summary>
        OnlinePayment = 0,
        /// <summary>
        /// 货到付款
        /// </summary>
        CashOnDelivery = 1,
        /// <summary>
        /// 线下转账
        /// </summary>
        OfflineTransfer = 2
    }
}
